package plus.easydo.server.user.mp.controller;

import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.server.user.mp.constant.UserConstants;
import plus.easydo.server.user.mp.entity.SysConfig;
import plus.easydo.server.user.mp.service.ISysConfigService;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "参数配置")
@SystemLog("参数配置")
@RestController
@RequestMapping("/config")
public class SysConfigController extends MPBaseController {

    @Resource
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("hasPermission('system:config:list')")
    @GetMapping("/list")
    public R<List<SysConfig>> list(SysConfig config) {
        List<SysConfig> list = configService.selectConfigList(config);
        return ok(list);
    }

//    @PreAuthorize("hasPermission('system:config:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysConfig config) throws IOException
//    {
//        List<SysConfig> list = configService.selectConfigList(config);
//        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
//        util.exportExcel(response, list, "参数数据");
//    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public R<Object> getInfo(@PathVariable Long configId) {
        return ok(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}')")
    public R<Object> getConfigKey(@PathVariable String configKey) {
        return ok(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @SystemLog(value = "新增参数配置",saveLog = true)
    @PreAuthorize("hasPermission('system:config:add')")
    @PostMapping
    public R<Object> add(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return fail("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(Oauth2Utils.getUserName());
        return opResult(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @SystemLog(value = "修改参数配置",saveLog = true)
    @PreAuthorize("hasPermission('system:config:edit')")
    @PutMapping
    public R<Object> edit(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return okMsg("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(Oauth2Utils.getUserName());
        return opResult(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @SystemLog(value = "删除参数配置",saveLog = true)
    @PreAuthorize("hasPermission('system:config:remove')")
    @DeleteMapping("/{configIds}")
    public R<Object> remove(@PathVariable Long[] configIds) {
        return opResult(configService.deleteConfigByIds(configIds));
    }

    /**
     * 清空缓存
     */
    @SystemLog(value = "清空系统参数缓存",saveLog = true)
    @PreAuthorize("hasPermission('system:config:remove')")
    @DeleteMapping("/clearCache")
    public R<Object> clearCache() {
        configService.clearCache();
        return ok();
    }
}
